const app = getApp();
Page({
  data: {
    list: [],
    createQrcode: {
      show: false,
      roomId: "", //房号id 用于详情获取二维码
      address: "", //房号地址 用于详情显示
      times: [1, 2], // 有效期时间选项
    },
  },
  // 生成二维码并去详情
  goCreateQrcodeDetails(e) {
    let roomId = this.data.createQrcode.roomId,
      time = e.currentTarget.dataset.time;

    app.call.getData(
      `wxapp/getVisitorCode?room_id=${roomId}&expiration_date=${time}`,
      (res) => {
        let data = res.data,
          url = data.url,
          createTime = data.create_time,
          expirationDate = data.expiration_date;
        // 判断是否过期
        let isExpiration = 0;
        if (new Date() > new Date(expirationDate)) isExpiration = 1;
        // 生成参数对象
        data = { url, createTime, expirationDate, isExpiration, address: this.data.createQrcode.address };
        url = `/pages/qrcode/details?data=${JSON.stringify(data)}`;
        this.setData({ "createQrcode.show": false });
        // 加延迟是为了修复 page-container 锁死返回的bug
        setTimeout(() => {
          wx.navigateTo({
            url
          });
        }, 200)
      }
    );
  },
  // 打开生成二维码
  openCreateQrcode(e) {
    let room = e.currentTarget.dataset.room,
      address = room.residential_name + room.build_name + room.room_name;
    this.setData({
      "createQrcode.show": true,
      "createQrcode.roomId": room.room_id,
      "createQrcode.address": address,
    });
  },
  // 监听页面加载
  onLoad(options) {
    this.getHouseNumberList();
  },
  // 获取房号列表
  getHouseNumberList() {
    app.call.request("wxapp/userRoom", {}, (res) => {
      let data = res.data;
      this.setData({
        list: data,
      });
    });
  },
});
